/*
 * simes - simulador de escalonamento de processos
 * Copyright (C) 2006  Pablo A. Lerina Rodrigues
 *                     Armando Zaro
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "colors.h"

/*
 * FIFO Stack
 * Retira elemento da pilha, mas em ordem inversa.
 * (nao queremos que "fim" seja o primeiro comando,
 *  entao vamos comecar retirando os nodos da base)
 */
t_snode *pop(t_stack *stack) {
	t_snode *tmp;
	if (stack->base != NULL) {
		// se a pilha nao estiver vazia
		tmp = malloc(sizeof(t_snode));
		tmp = stack->base;
		stack->base = tmp->up;
	}
	return tmp;
}

/*
 * Poem node na pilha.
 */
void push(t_snode *node, t_stack *stack) {
	if (stack->base == NULL) {
		// primeiro elemento da pilha
		node->up = NULL;
		node->down = NULL;
		stack->base = node;
	} else {
		node->up = NULL;
		stack->top->up = node;
		node->down = stack->top;
	}
	stack->top = node;
#ifdef DEBUG_STACK
	t_snode tmp = malloc(sizeof(t_snode));
	tmp = stack->base;
	do {
		printf("%s %d %d ", RED, tmp->comando, tmp->valor);
		if (tmp->down != NULL && tmp->up != NULL)
			printf(" <-> %s ", NORMAL);
		else if (tmp->up == NULL)
			printf(" %s ", NORMAL);
		else
			printf(" -> %s ", NORMAL);
	} while ((tmp = tmp->up) != 0);
	free(tmp);
	printf("\n");
#endif
}
